//
//  MyMapAllViewController.m
//  markmap
//
//  Created by 甘世清 on 2024/3/25.
//

#import "MyMapAllViewController.h"
#import "LocationPermissionView.h"
@interface MyMapAllViewController ()<MKMapViewDelegate>
@property (nonatomic , strong) MKMapView * mapView;
@property (nonatomic , strong) SQCustomButton * dingwei;
@end

@implementation MyMapAllViewController
- (void)SQ_viewWillAppear:(BOOL)animated{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self SQ_Header];
    });
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = SQColor(@"#2F4F4F");
    self.zx_hideBaseNavBar = YES;
    [LocationPermissionView SQ_Show:self.view];
}
 
- (void)SQ_UIViewForSelfView{
    [self.view addSubview:self.mapView];
    [self.view addSubview:self.dingwei];
    [self.dingwei mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.view.mas_bottom).offset(-FootHeight-100);
           make.size.mas_equalTo(50);
            make.right.equalTo(self.view.mas_right).offset(-10);
    }];
    WS(weakSelf);
    [self.dingwei setTouchBlock:^(SQCustomButton * _Nonnull button) {
        CLLocationCoordinate2D coordinate2D = CLLocationCoordinate2DMake([UserModel share].lat, [UserModel share].lng);
           float zoomLevel = 0.005;
           MKCoordinateRegion region = MKCoordinateRegionMake(coordinate2D, MKCoordinateSpanMake(zoomLevel, zoomLevel));
        [weakSelf.mapView setRegion:[weakSelf.mapView regionThatFits:region] animated:YES];
    }];
}

- (void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"1" forKey:@"current"];
    [dic setObject:@"1000" forKey:@"size"];
    [HttpTool post:Url22 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        [self.jsonArray removeAllObjects];
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [PointModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        if (array.count <= 0) {
            return;
        }  
        for (PointModel * mo in array) {
            BaseAnntation *pointAnnotation = [[BaseAnntation alloc] init];
            pointAnnotation.coordinate = CLLocationCoordinate2DMake(mo.lat, mo.lng);
            pointAnnotation.model = mo;
            [self.jsonArray addObject:pointAnnotation];
        }
        [self.mapView removeAnnotations:self.mapView.annotations];
        [self.mapView addAnnotations:self.jsonArray];
        [self.mapView showAnnotations:self.jsonArray animated:YES];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
   
}

//设置大头针
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    if (![annotation isKindOfClass:[BaseAnntation class]]) {// 过滤掉MKUserLocation
        return nil;
    }
    BaseAnntation* ann = (BaseAnntation *)annotation;
    BaseAnnotationView *annotationView = (BaseAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:ann.model.annotationViewIdentifier];
    if (!annotationView) {
       annotationView = [[BaseAnnotationView alloc] initWithAnnotation:ann
                                                     reuseIdentifier:ann.model.annotationViewIdentifier];
        annotationView.opaque = NO;
    }
    annotationView.model = ann.model;
    return annotationView;
}

- (void)dealloc {
    _mapView.showsUserLocation = NO;
    _mapView.userTrackingMode  = MKUserTrackingModeNone;
    [_mapView.layer removeAllAnimations];
    [_mapView removeAnnotations:_mapView.annotations];
    [_mapView removeOverlays:_mapView.overlays];
    [_mapView removeFromSuperview];
    _mapView.delegate = nil;
    _mapView = nil;
}


/** 懒加载地图视图 */
- (MKMapView *)mapView{
    if (!_mapView) {
        _mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, SCREEN_HEIGHT)];
        /** 设置地图类型 */
        _mapView.mapType = MKMapTypeMutedStandard;
        _mapView.userTrackingMode = MKUserTrackingModeFollow;
        //设置地图可缩放
        _mapView.zoomEnabled = YES;
        //设置地图可滚动
        _mapView.scrollEnabled = YES;
        //设置地图可旋转
        _mapView.rotateEnabled = YES;
        //设置显示用户显示位置
        _mapView.showsUserLocation = YES;
        //为MKMapView设置delegate
        _mapView.delegate = self;
        _mapView.showsBuildings = YES;
        _mapView.showsTraffic = YES;
    }
    return _mapView;
}
- (SQCustomButton *)dingwei{
    if (!_dingwei) {
        _dingwei = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(30, 30) midmargin:0];
        _dingwei.image = SQGetImage(@"zishendingwei");
    }
    return _dingwei;
}
@end
